package com.sylar.lelepub.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

public class QuickShortcut extends ImageView{
	private Paint mPaint;
	private Drawable mDrawable;
	
	public QuickShortcut(Context context) {
		super(context);
		mPaint = new Paint();
		mPaint.setColor(Color.BLACK);
		mPaint.setFilterBitmap(true);
		mPaint.setAntiAlias(true);
		
		float radius =  5.0f * getContext().getResources().getDisplayMetrics().density;
        BlurMaskFilter bmFilter = new BlurMaskFilter(radius, BlurMaskFilter.Blur.NORMAL);
        
        mPaint.setMaskFilter(bmFilter);
		
	}
	
	public QuickShortcut(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		mPaint = new Paint();
		mPaint.setColor(Color.BLACK);
		mPaint.setAlpha(50);
		mPaint.setFilterBitmap(true);
		mPaint.setAntiAlias(true);
		//ģ��Ч��
//		float radius =  5.0f * getContext().getResources().getDisplayMetrics().density;
//        BlurMaskFilter bmFilter = new BlurMaskFilter(radius, BlurMaskFilter.Blur.NORMAL);
//        mPaint.setMaskFilter(bmFilter);
	
	}
	
	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		mDrawable = getDrawable();
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (isPressed() || isFocused()) {
			if (mDrawable instanceof BitmapDrawable) {
				Bitmap temp = ((BitmapDrawable) mDrawable).getBitmap();
				Bitmap mBitmap = temp.extractAlpha(mPaint, null);
				int width = getWidth();
				int mWidth = mBitmap.getWidth();
				int height = getHeight();
				int mHeight = mBitmap.getHeight();
				canvas.drawBitmap(mBitmap, (width - mWidth) / 2, (height - mHeight) / 2, mPaint);
			}
		}
	}

	@Override
	protected void drawableStateChanged() {
		super.drawableStateChanged();
		postInvalidate();
	}
	
	public StateListDrawable setbg(Bitmap... bmp) {
		StateListDrawable bg = new StateListDrawable();
		Drawable normal = new BitmapDrawable(bmp[0]);
		Drawable selected = new BitmapDrawable(bmp[1]);
		Drawable pressed = new BitmapDrawable(bmp[2]);
		bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);
		bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);
		bg.addState(View.ENABLED_STATE_SET, normal);
		bg.addState(View.FOCUSED_STATE_SET, selected);
		bg.addState(View.EMPTY_STATE_SET, normal);
		return bg;
	}
}
